{% extends "../../base.html" %}
{% block content %}

  <script language="javascript" type="text/javascript">

    function getCarLink(_p)
    {
      var html = '<a href="/fuel/car?pid=' + _p +'">' + _p + '</a>';
      document.write(html);
      return true;
    }

    function getCarMark(_active, _t)
    {
      var html = '';
      if (_active)
        if (_t == 1)
          html = '<div class="marked">';
        else
          html = '</div>';
      document.write(html);
      return true;
    }

    var car_active = '0';

    function myClick(_num)
    {
      var frm = document.getElementById("fld_form");
      var act = frm.elements["action"];
      switch (_num)
      {
        case 1: act.value = "insert"; break;
        case 2: act.value = "update"; break;
        case 3: act.value = "delete"; break;
        case 4: act.value = "cancel"; break;
        case 5: act.value = "back";   break;
        default: act.value = "undef"; 
      }
      frm.elements["car_act"].value = car_active;
      frm.Submit();
      return true;
    }

    bEdit = false;

    function check()
    {
      if (document.getElementById('fld_form').elements['active'].checked)
        car_active = '1';
      else
        car_active = '0';

      var name = document.getElementById('fld_form').elements['name'].value;

      var status = '';
      if (name == '' || name == '?')
        status = 'Не задано название автомобиля';
      
      document.getElementById('status').innerHTML = status;
      
      var x = document.getElementById('button');
      
      if (status != '')
      {
        if (!bEdit)
          x.innerHTML = '';
        else
          x.innerHTML = '<input type="submit" class="LiteButton" id="delete" value="Удалить"   onClick="myClick(3)" />' +
                        '<input type="submit" class="LiteButton" id="common" value="Отменить"  onClick="myClick(4)" />';
      }
      else
      {
        if (!bEdit)
          x.innerHTML = '<input type="submit" class="LiteButton" id="update" value="Добавить"  onClick="myClick(1)" />';
        else
          x.innerHTML = '<input type="submit" class="LiteButton" id="update" value="Сохранить" onClick="myClick(2)" />' +
                        '<input type="submit" class="LiteButton" id="delete" value="Удалить"   onClick="myClick(3)" />' +
                        '<input type="submit" class="LiteButton" id="common" value="Отменить"  onClick="myClick(4)" />';
      }
    }

  </script>

  <div id="gmenu" class="menu"> <a href="/">{{ app_text }}</a> | <a href="/fuel">{{ fuel_text }}</a> </div>
  <div class="PageTitle">  {{page_title}}  </div>
  <div class="PageStatus"> {{page_status}} </div>

  <form id="fld_form" action="/fuel/car" method="post">
    <input name="pid"     type="hidden" value="{{cur.pid}}">
    <input name="action"  type="hidden" value="none">
    <input name="car_act" type="hidden" value="0">
    <table class="fl1">
      <tbody>
      {% if edit_rec %} 
      <script language="javascript" type="text/javascript"> bEdit = true; </script>
      <tr><td class="fl1"> ID:         </td><td class="fl2"> {{ cur.pid }} </td> </tr>
      {% else %}
      <script language="javascript" type="text/javascript"> bEdit = false; </script>
      {% endif %}
      <tr><td class="fl1"> Название:  </td><td class="fl2"> <input name="name"  size="15" type="text" value="{{ cur.name  }}" onChange="check()" /> </td> </tr>
      <tr><td class="fl1"> Гос.номер: </td><td class="fl2"> <input name="plate" size="15" type="text" value="{{ cur.plate }}" onChange="check()" /> </td> </tr>
      <tr><td class="fl1"> Активный:  </td><td class="fl2"> <input name="active" type="checkbox" {% if cur.active %} checked {% endif %} onChange="check()" /> </td> </tr>
    </tbody></table>

    <span id="button"> </span> 
    <span id="status" class="PageStatus"> </span> 
    <script language="javascript" type="text/javascript"> check() </script>
  </form>

  {% if cars.count %}
  <table class="work">
    <tbody><tr>
      <th class="work"> ID        </th>
      <th class="work"> Название  </th>
      <th class="work"> Гос.номер </th>
    </tr>
    {% for p in cars %}
    <tr>
      <td class="pid">  <script language="javascript" type="text/javascript">getCarLink({{ p.pid }})</script> </td>
      <td class="work"> <script language="javascript" type="text/javascript">getCarMark({{ p.active }}, 1)</script>
                        {{ p.name }}
                        <script language="javascript" type="text/javascript">getCarMark({{ p.active }}, 2)</script></td>
      <td class="work"> <script language="javascript" type="text/javascript">getCarMark({{ p.active }}, 1)</script>
                        {{ p.plate }}
                        <script language="javascript" type="text/javascript">getCarMark({{ p.active }}, 2)</script></td>
    </tr>
    {% endfor %}
  </tbody></table>
  {% endif %}

  <br/>Всего автомобилей: {{ cars.count }}<br/>

{% endblock %}
